<template>
  <div id="app">
    <h1>Type name and mark who is student</h1>
    <!-- 添加表单 -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="addContainer"
    >
      <el-form-item label="Name" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="IsStudent">
        <el-switch v-model="ruleForm.isStudent"></el-switch>
      </el-form-item>
      <el-form-item label="CreateTime" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="ruleForm.date1"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-time-picker
              placeholder="选择时间"
              v-model="ruleForm.date2"
              style="width: 100%"
            ></el-time-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="Hobby" prop="hobby">
        <el-input type="textarea" v-model="ruleForm.hobby"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 渲染表格数据 -->
    <el-table :data="person" border style="width: 60%" class="table">
      <el-table-column prop="id" label="Id" width="50"> </el-table-column>
      <el-table-column prop="name" label="Name" width="100"> </el-table-column>
      <el-table-column prop="isStudent" label="IsStudent">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isStudent"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="hobby" label="hobby"></el-table-column>
      <el-table-column prop="createTime" label="CreateTime">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="editRow(scope.row, scope.$index)"
            >编辑</el-button
          >
          <el-popconfirm
            confirm-button-text="YES"
            cancel-button-text="NO"
            icon="el-icon-info"
            icon-color="red"
            title="Are you sure???"
            @confirm="deleteRow(scope.$index, person)"
          >
            <el-button slot="reference" size="small">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改数据对话框 -->
    <el-dialog title="EDIT PERSON" :visible.sync="dialogVisible" width="50%">
      <el-form
        :model="rowData"
        :rules="rules"
        ref="rowData"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="Name" prop="name">
          <el-input v-model="rowData.name"></el-input>
        </el-form-item>
        <el-form-item label="Hobby" prop="hobby">
          <el-input type="textarea" v-model="rowData.hobby"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="offDialogView('rowData')">Cancel</el-button>
        <el-button type="primary" @click="edit('rowData')">Sure</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Storage from "./localStorage";
export default {
  name: "App",
  data() {
    return {
      person: [
        {
          id: 1,
          name: "yzx",
          isStudent: true,
          createTime: "2023-08-01 21:27:08",
          hobby: "打球",
        },
        {
          id: 2,
          name: "yao",
          isStudent: false,
          createTime: "2023-08-01 21:27:08",
          hobby: "打球g",
        },
        {
          id: 3,
          name: "yao",
          isStudent: false,
          createTime: "2023-08-01 21:27:08",
          hobby: "rap",
        },
      ],
      //添加表单的信息
      ruleForm: {
        name: "", //姓名
        date1: "", //创建时间
        date2: "",
        isStudent: false, //是否为学生
        hobby: "", //hobby
      },
      rules: {
        name: [
          { required: true, message: "请输入你的姓名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        date2: [
          {
            type: "date",
            required: true,
            message: "请选择时间",
            trigger: "change",
          },
        ],
        hobby: [{ required: true, message: "请填写你的爱好", trigger: "blur" }],
      },
      //修改数据对话框
      dialogVisible: false,
      // 修改表单数据
      rowData: {
        name: "",
        hobby: "",
      },
      rowIndex: 0,
    };
  },
  created() {
    this.initData();
  },
  methods: {
    //添加表单的方法
    dataFormat1(originVal) {
      const dt = new Date(originVal);
      const y = dt.getFullYear();
      const m = (dt.getMonth() + 1 + "").padStart(2, "0");
      const d = (dt.getDay() + "").padStart(2, "0");
      return `${y}-${m}-${d} `;
    },
    dataFormat2(originVal) {
      const dt = new Date(originVal);
      const hh = (dt.getHours() + "").padStart(2, "0");
      const mm = (dt.getMinutes() + "").padStart(2, "0");
      const ss = (dt.getSeconds() + "").padStart(2, "0");
      return `${hh}:${mm}:${ss}`;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let createTime =
            this.dataFormat1(this.ruleForm.date1) +
            this.dataFormat2(this.ruleForm.date2);
          this.person.push({
            id: this.person.length
              ? this.person[this.person.length - 1].id + 1
              : 1,
            name: this.ruleForm.name,
            isStudent: this.ruleForm.isStudent,
            hobby: this.ruleForm.hobby,
            createTime: createTime,
          });
          this.$message({
            type: "success",
            message: "ADD SUCCESS",
            center: true,
          });
          this.resetForm(formName);
        } else {
          this.$message({
            type: "error",
            message: "As what you required!!!",
            center: true,
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //删除数据行
    deleteRow(index, rows) {
      rows.splice(index, 1);
      this.$message({
        type: "success",
        message: "REMOVE SUCCESS",
        center: true,
      });
    },
    //修改数据行
    editRow(row, index) {
      //这里一定要注意，不能直接=,因为是对象
      this.rowData.name = row.name;
      this.rowData.hobby = row.hobby;
      this.rowIndex = index;
      this.dialogVisible = true;
    },
    offDialogView(formName) {
      this.dialogVisible = false;
      this.$refs[formName].resetFields();
    },
    edit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.person[this.rowIndex].name = this.rowData.name;
          this.person[this.rowIndex].hobby = this.rowData.hobby;
          this.dialogVisible = false;
          this.$message({
            type: "success",
            message: "EDIT SUCCESS...",
            center: true,
          });
        } else {
          return false;
        }
      });
    },
    //初始数据函数
    initData() {
      this.person = Storage.fetch();
    },
  },
  watch: {
    person: {
      handler: function (person) {
        Storage.save(person); //监控变化，将新增的值存入 localStorage 里
      },
      deep: true,
    },
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.table {
  margin: auto;
}
.addContainer {
  width: 500px;
  margin: auto;
}
</style>
